package java3_24.StringHomework10;

import org.omg.CORBA.CODESET_INCOMPATIBLE;

import javax.print.DocFlavor;
import java.util.Scanner;

/**
 * *10:输入一个正整数，转换成汉语的数字读法，
 * 比如，3000，读作 三千；3004，读作 三千零四；
 * 10010，读作 一万零一十；15读作十五。。。
 */
public class StringHomework10 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个正整数：50020");
        String x = scanner.next();
        char[] b = x.toCharArray();
        StringBuffer stringBuffer = new StringBuffer();
        // 5 0 0 2 0
        for (int i = 0; i < b.length; i++) {
            //5
            String num = String.valueOf(b[i]);
            //五
            String numToChinese = numToChinese(Integer.parseInt(num), Exchange.getExchange());
            //万
            String count = count(b.length-i);
            if (0 == Integer.parseInt(num)){
                int indexOf = stringBuffer.lastIndexOf("零");
                if (indexOf != stringBuffer.length()-1){
                    stringBuffer.append(numToChinese);
                }else if (indexOf == -1){
                    stringBuffer.append(numToChinese);
                }
            }else {
                stringBuffer.append(numToChinese);
                stringBuffer.append(count);
            }
        }
        String asd =  stringBuffer.substring(stringBuffer.length()-1);
        if ("零".equals(asd)){
            stringBuffer.deleteCharAt(stringBuffer.length()-1);
        }
        /*int indexOf = stringBuffer.lastIndexOf("零");
        if (indexOf == stringBuffer.length()-1){
            stringBuffer.deleteCharAt(indexOf);
        }*/
        System.out.println(stringBuffer.toString());

    }
    public static String count(int x){
        switch (x){
            case 1:{
                String str = "";
                return str;
            }
            case 2:{
                String str = "十";
                return str;
            }
            case 3:{
                String str = "百";
                return str;
            }
            case 4:{
                String str = "千";
                return str;
            }
            case 5:{
                String str = "万";
                return str;
            }
            default:return null;
        }
    }

    public static String numToChinese(int num, Exchange[] exchanges) {
        for (int i=0;i<exchanges.length;i++){
            Exchange exchange = exchanges[i];
            if (num==exchange.digital){
                return exchange.word;
            }
        }
        return null;
    }
}
